﻿using System.Collections.Generic;

namespace Zo.Collections
{
    /// <summary>
    /// URL 参数字典。
    /// </summary>
    public class UrlDictionary
    {
        /// <summary>
        /// 字典信息。
        /// </summary>
        private readonly Dictionary<string, string> _dictionary = new Dictionary<string, string>();

        /// <summary>
        ///
        /// </summary>
        public ICollection<string> Keys
        {
            get { return _dictionary.Keys; }
        }

        /// <summary>
        ///
        /// </summary>
        public ICollection<string> Values
        {
            get { return _dictionary.Values; }
        }

        public int Count
        {
            get { return _dictionary.Count; }
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public string this[string key]
        {
            get
            {
                if (string.IsNullOrWhiteSpace(key))
                    return null;

                key = key.ToLower();

                return _dictionary.ContainsKey(key) ? _dictionary[key] : null;
            }
            set
            {
                if (string.IsNullOrWhiteSpace(key))
                    return;

                key = key.ToLower();

                _dictionary[key] = value;
            }
        }

        /// <summary>
        /// 判断元素是否存在。
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public bool ContainsKey(string key)
        {
            if (string.IsNullOrWhiteSpace(key))
                return false;

            key = key.ToLower();

            return _dictionary.ContainsKey(key);
        }

        public bool ContainsValue(string value)
        {
            return _dictionary.ContainsValue(value);
        }

        /// <summary>
        /// 增加元素。
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public void Add(string key, string value)
        {
            if (string.IsNullOrWhiteSpace(key))
                return;

            key = key.ToLower();

            _dictionary.Add(key, value);
        }

        /// <summary>
        /// 移除指定 KEY 的元素。
        /// </summary>
        /// <param name="key"></param>
        /// <returns>是否成功。</returns>
        public bool Remove(string key)
        {
            if (string.IsNullOrWhiteSpace(key))
                return false;

            key = key.ToLower();

            return _dictionary.Remove(key);
        }

        /// <summary>
        /// 获得元素。
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool TryGetValue(string key, out string value)
        {
            if (string.IsNullOrWhiteSpace(key))
            {
                value = null;
                return false;
            }

            key = key.ToLower();

            return _dictionary.TryGetValue(key, out value);
        }

        public void Clear()
        {
            _dictionary.Clear();
        }
    }
}